package cn.iocoder.yudao.module.train.controller.admin.coursecommodity;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCommodityDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.service.coursecommodity.CourseCommodityService;

@Tag(name = "管理后台 - 课程商品管理")
@RestController
@RequestMapping("/train/course-commodity")
@Validated
public class CourseCommodityController {

    @Resource
    private CourseCommodityService courseCommodityService;

    @PostMapping("/create")
    @Operation(summary = "创建课程商品管理")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:create')")
    public CommonResult<Long> createCourseCommodity(@Valid @RequestBody CourseCommoditySaveReqVO createReqVO) {
        return success(courseCommodityService.createCourseCommodity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程商品管理")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:update')")
    public CommonResult<Boolean> updateCourseCommodity(@Valid @RequestBody CourseCommoditySaveReqVO updateReqVO) {
        courseCommodityService.updateCourseCommodity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程商品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:course-commodity:delete')")
    public CommonResult<Boolean> deleteCourseCommodity(@RequestParam("id") Long id) {
        courseCommodityService.deleteCourseCommodity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程商品管理")
                @PreAuthorize("@ss.hasPermission('train:course-commodity:delete')")
    public CommonResult<Boolean> deleteCourseCommodityList(@RequestParam("ids") List<Long> ids) {
        courseCommodityService.deleteCourseCommodityListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程商品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:query')")
    public CommonResult<CourseCommodityRespVO> getCourseCommodity(@RequestParam("id") Long id) {
        CourseCommodityDTO courseCommodity = courseCommodityService.getCourseCommodity(id);
        return success(BeanUtils.toBean(courseCommodity, CourseCommodityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程商品管理分页")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:query')")
    public CommonResult<PageResult<CourseCommodityRespVO>> getCourseCommodityPage(@Valid CourseCommodityPageReqVO pageReqVO) {
        PageResult<CourseCommodityDTO> pageResult = courseCommodityService.getCourseCommodityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseCommodityRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程商品管理列表")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:query')")
    public CommonResult<List<CourseCommodityRespVO>> getCourseCommodityList(@Valid CourseCommodityPageReqVO pageReqVO) {
        List<CourseCommodityDTO> listResult = courseCommodityService.getCourseCommodityList(pageReqVO);
        return success(BeanUtils.toBean(listResult, CourseCommodityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程商品管理 Excel")
    @PreAuthorize("@ss.hasPermission('train:course-commodity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseCommodityExcel(@Valid CourseCommodityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseCommodityDTO> list = courseCommodityService.getCourseCommodityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程商品管理.xls", "数据", CourseCommodityRespVO.class,
                        BeanUtils.toBean(list, CourseCommodityRespVO.class));
    }

}
